package com.sunday.aomi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.adapter.CommenAdapter;
import com.sunday.common.adapter.ViewHolder;
import com.sunday.common.utils.SpannalbeStringUtils;
import com.sunday.common.widgets.NoScrollListview;
import com.sunday.aomi.R;

import com.sunday.aomi.ui.order.SellOrderDetailActivity;
import com.sunday.member.constv.ConstValue;

import com.sunday.member.entity.order.OrderItem;
import com.sunday.member.entity.order.OrderProduct;

import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/6/7.
 */

public class SellOrderListAdapter extends RecyclerView.Adapter<SellOrderListAdapter.ListHolder> {


    private Context mContext;
    private List<OrderItem> dataSet;
    private View.OnClickListener onClickListener;

    public SellOrderListAdapter(Context context, List<OrderItem> datas) {
        this.mContext = context;
        this.dataSet = datas;
    }


    @Override
    public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_sell_order_item, null);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(ListHolder orderHolder, int position) {
        OrderItem item = dataSet.get(position);
        int num = 0;
        if (item.getProducts() != null && item.getProducts().size() > 0) {
            orderHolder.datas.clear();
            orderHolder.datas.addAll(item.getProducts());
            orderHolder.adapter.notifyDataSetChanged();
            for (OrderProduct product : item.getProducts()) {
                num += product.getNum();
            }
        }

        orderHolder.orderNo.setText(String.format("订单号：%s", item.getOrderNo()));
        orderHolder.orderMsg.setVisibility(TextUtils.isEmpty(item.getRemark()) ? View.GONE : View.VISIBLE);
        orderHolder.orderMsg.setText(SpannalbeStringUtils.setTextColor("留言：", mContext.getResources().getColor(R.color.black_6)));
        orderHolder.orderMsg.append(SpannalbeStringUtils.setTextColor(String.format("%s", item.getRemark())
                , mContext.getResources().getColor(R.color.black_9)));
        orderHolder.totalNum.setText(SpannalbeStringUtils.setTextColor("共计商品：", mContext.getResources().getColor(R.color.black_3)));
        orderHolder.totalNum.append(SpannalbeStringUtils.setTextColor(String.format("%d件", num), mContext.getResources().getColor(R.color.price_yellow)));

        orderHolder.totalMoney.setText(SpannalbeStringUtils.setTextColor("应付款：", mContext.getResources().getColor(R.color.black_3)));
        orderHolder.totalMoney.append(SpannalbeStringUtils.setTextColor(String.format("￥%s", item.getPayAmount().setScale(2, RoundingMode.HALF_UP)), mContext.getResources().getColor(R.color.price_yellow)));
        orderHolder.tvStatus.setText(item.getOrderStatusStr());

        orderHolder.name.setText(item.getReceiveName());
        orderHolder.phoneNumber.setText(item.getReceiveMobile());
        orderHolder.address.setText(item.getReceiveAddress());
        orderHolder.orderTime.setText(item.getCreateDate());

        switch (item.getOrderStatus()) {
            case ConstValue.STA_TWO:
                if (item.getSendType()!=null&&item.getSendType()==2){
                    orderHolder.orderBtn1.setText("用户自提");
                }else {
                    orderHolder.orderBtn1.setText("配送");
                }
                orderHolder.btnLayout.setVisibility(View.VISIBLE);
                break;
            case ConstValue.STA_THREE:
                orderHolder.btnLayout.setVisibility(View.GONE);
                break;
            default:
                orderHolder.btnLayout.setVisibility(View.GONE);
        }

        orderHolder.orderBtn1.setTag(item);
        orderHolder.itemView.setTag(item);
        orderHolder.listView.setTag(item);
        orderHolder.orderBtn1.setOnClickListener(onClickListener);
        orderHolder.itemView.setOnClickListener(onClickListener);
    }


    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    class ListHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.order_no)
        TextView orderNo;
        @Bind(R.id.tvStatus)
        TextView tvStatus;
        @Bind(R.id.order_msg)
        TextView orderMsg;
        @Bind(R.id.no_scroll_list_view)
        NoScrollListview listView;
        @Bind(R.id.total_num)
        TextView totalNum;
        @Bind(R.id.total_money)
        TextView totalMoney;
        @Bind(R.id.rl_total_layout)
        RelativeLayout rlTotalLayout;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.phone_number)
        TextView phoneNumber;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.order_btn2)
        Button orderBtn2;
        @Bind(R.id.order_btn1)
        Button orderBtn1;
        @Bind(R.id.order_time)
        TextView orderTime;
        @Bind(R.id.btn_layout)
        RelativeLayout btnLayout;
        @Bind(R.id.item_view)
        LinearLayout itemView;

        private CommenAdapter<OrderProduct> adapter;
        private List<OrderProduct> datas = new ArrayList<>();

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            adapter = new CommenAdapter<OrderProduct>(mContext, datas, R.layout.list_order_confirm_product) {
                @Override
                public void convert(ViewHolder helper, OrderProduct item, int position) {
                    bind(helper, item);
                }
            };
            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    OrderItem orderItem = (OrderItem) listView.getTag();
                    Intent intent = new Intent(mContext, SellOrderDetailActivity.class);
                    intent.putExtra("orderId", orderItem.getId());
                    mContext.startActivity(intent);
                }
            });
        }
    }

    private void bind(ViewHolder helper, OrderProduct item) {
        ImageView imageView = helper.getView(R.id.product_img);
        if (!TextUtils.isEmpty(item.getImage())) {
            Picasso.with(mContext)
                    .load(item.getImage())
                    .error(R.mipmap.ic_default)
                    .resize(150, 150)
                    .into(imageView);
        }
        ((TextView) helper.getView(R.id.product_title)).setText(item.getName());
        ((TextView) helper.getView(R.id.product_price)).setText(String.format("￥%s", item.getPrice().setScale(2, RoundingMode.HALF_UP)));
        ((TextView) helper.getView(R.id.product_num)).setText(String.format("X%d", item.getNum()));
        ((TextView) helper.getView(R.id.product_spec)).setText(item.getStandard());
    }

}
